//
// Created by hjie on 23-7-18.
//
#include "./../base/link.h"

void ProducerLinkList(LinkList & link_list, int number)
{
    srand(time(NULL) + rand() % 1000);

    link_list.head_node = new LinkNode();
    link_list.tail_node = link_list.head_node;

    link_list.head_node->data = rand() % 50;
    link_list.head_node->next_node = nullptr;
    link_list.head_node->prev_node = nullptr;

    for (int index = 0; index < number - 1; index++)
    {
        int data = rand() % 50;
        LinkNode * node = new LinkNode();
        node->data = data;
        node->next_node = nullptr;
        node->prev_node = nullptr;

        link_list.tail_node->next_node = node;
        node->prev_node = link_list.tail_node;

        link_list.tail_node = node;
    }
    printf("producer data : ");
    LinkNode * ptr = link_list.head_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");
}

LinkNode * TestCode(LinkNode * node);

void ReverseListLink()
{
    int length = 20;
    LinkList list;

    ProducerLinkList(list, length);

    LinkNode * tail_node = list.tail_node;
    LinkNode * ptr = list.head_node;

    TestCode(list.head_node);

    printf("result data  : ");
    ptr = list.tail_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");

    ptr = list.head_node;
    list.head_node = list.tail_node;
    list.tail_node = ptr;

    LinkNode * tail = list.tail_node;
    ptr = list.head_node;

    while (ptr)
    {
        if (ptr->next_node && ptr->next_node == tail)
        {
            tail->next_node = ptr;
            ptr->next_node = nullptr;
            ptr = list.head_node;
            tail = tail->next_node;
            continue;
        }
        ptr = ptr->next_node;
    }

    printf("result data  : ");
    ptr = list.tail_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");
}
//
LinkNode * TestCode(LinkNode * node)
{
    if (node)
    {
        LinkNode * ptr =  TestCode(node->next_node);
        if (ptr)
        {
            ptr->next_node = node;
            node->next_node = nullptr;
            return node;
        }
    }
    return node;
}

LinkNode * TestCode(LinkNode * node, LinkNode * target)
{
    if (node == target)
    {
        node->next_node = nullptr;
        return node;
    }
    LinkNode * ptr = TestCode(node->next_node, target);
    if (ptr)
    {
        ptr->next_node  = node;
        node->next_node = nullptr;
    }
    return node;
}

void ReversePartLinkList()
{
    int length = 20;
    LinkList list;

    ProducerLinkList(list, length);

    int left  = 10;
    int right = 15;

    LinkNode * ptr = list.head_node;
    LinkNode * record_node_one = nullptr;
    LinkNode * record_node_one_next = nullptr;
    LinkNode * record_node_two = nullptr;
    LinkNode * record_node_two_next = nullptr;
    length = 1;
    while (ptr)
    {
        if (length == left)
        {
            record_node_one_next = ptr;
        }
        else if (length + 1 == left)
        {
            record_node_one = ptr;
        }
        if (length == right)
        {
            record_node_two = ptr;
            record_node_two_next = record_node_two->next_node;
        }
        length++;
        ptr = ptr->next_node;
    }
    ptr = TestCode(record_node_one_next, record_node_two);
    ptr->next_node = record_node_two_next;

    record_node_one->next_node = record_node_two;

    printf("result data : ");
    ptr = list.head_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");

}

int main(int argc, const char * argv[])
{
    // ReverseListLink();
    ReversePartLinkList();
    return 0;
}


